package com.nuc.kl.module.usermanager.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.module.usermanager.entity.Student;
import com.nuc.kl.module.usermanager.entity.Teacher;
import com.nuc.kl.module.usermanager.entity.User;
import com.nuc.kl.module.usermanager.service.LoginService;
import com.nuc.kl.module.usermanager.service.PrivilegeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
public class LoginAction extends ActionSupport{
	
	private String loginName;
	private String password;
	private int peopleFlag;
	
	
	@Resource
	private LoginService loginService;
	
	@Resource
	private PrivilegeService privilegeService;
	//登录 
	@Override
	public String execute() throws Exception {
		
		ActionContext.getContext().getSession().clear();
		
		String md5Digest = null;
		if(password != null){
			md5Digest = DigestUtils.md5Hex(password);
		}
		System.out.println("123123");
		
		//判断登录用户类别
		if(peopleFlag==1){ //管理员
			
				Object object = loginService.login(loginName, md5Digest, "User");
				if(object!=null){
					User user = (User)object;
					 List list = privilegeService.getEntityPrivilege(user);
					 		for (Object lis : list) {
								System.out.println(lis);
							}
						ActionContext.getContext().getSession().put("people", user);
						ActionContext.getContext().getSession().put("peopleFlag", 1);

					ActionContext.getContext().getSession().put("list",list);
					return SUCCESS;
				}
				
				addActionError("用户名或者密码错误!");
				return ERROR;
			
		}else if(peopleFlag==2){//教师
			Object object = loginService.login(loginName, md5Digest,"Teacher");
			if(object!=null){
				
				Teacher teacher = (Teacher)object;
				List list = new ArrayList();
				
				list.add("知识管理");
				
				list.add("查看知识点");
				list.add("查看参考资料");
				list.add("提交知识点(教师)");
				list.add("辅助教案生成(教师)");
				list.add("个人设置");
				list.add("个人信息管理");
				
				

				if(teacher.getIsExpert()==1){
					list.add("知识点审核");
					ActionContext.getContext().getSession().put("peopleFlag", 4);
				}else{
					ActionContext.getContext().getSession().put("peopleFlag", 2);
				}
				
				ActionContext.getContext().getSession().put("list",list);

				ActionContext.getContext().getSession().put("people", teacher);

				
				return SUCCESS;
			}

			addActionError("用户名或者密码错误!");
			return ERROR;
		}else if(peopleFlag==3){//学生
			Object object = loginService.login(loginName, md5Digest,"Student");
			if(object!=null){
				Student student =(Student)object;
				List list = new ArrayList();
				
				list.add("知识管理");
				list.add("查看知识点");
				list.add("查看参考资料");
				list.add("建立学习计划(学生)");
				list.add("个人设置");
				list.add("个人信息管理");

				ActionContext.getContext().getSession().put("people", student);
				ActionContext.getContext().getSession().put("peopleFlag", 3);

				ActionContext.getContext().getSession().put("list", list);
				return SUCCESS;
			}

			addActionError("用户名或者密码错误!");
			return ERROR;
			
		}else{
			return ERROR;
		}
		
		
		
	
	}
	
	
	public  String logout() throws Exception{
		
		ActionContext.getContext().getSession().clear();
		System.out.println("123123");
		return "login";
	}
	
	
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getPeopleFlag() {
		return peopleFlag;
	}
	public void setPeopleFlag(int peopleFlag) {
		this.peopleFlag = peopleFlag;
	}
	
	
}
